﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using Oracle.ManagedDataAccess.Client;

namespace CP.Repositories.GPM
{
    public class ButtonAuthorizeRepsitory
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Button_List> GetUnAuthBtns(int groupid, int webpartid)
        {
            string sql = string.Format(@"SELECT t1.*
                          FROM button_list t1
                         WHERE t1.webpart_id={1} 
                              and  NOT EXISTS (
                                  SELECT NULL
                                    FROM webpart_right_button t2
                                   WHERE t1.button_id = t2.button_id
                                     AND t2.GROUP_ID = {0}
                                     AND t2.webpart_id = {1})
                                order by t1.button_id ", groupid, webpartid);
            return db.ExecuteObject<Button_List>(sql);
        }

        public IList<Button_List> GetAuthBtns(int groupid, int webpartid)
        {
            string sql = string.Format(@"SELECT t1.*
                          FROM button_list t1,webpart_right_button t2
                                   WHERE t1.button_id = t2.button_id
                                     AND t1.webpart_id=t2.webpart_id
                                     AND t2.GROUP_ID = {0}
                                     AND t2.webpart_id = {1}
                                      order by t1.button_id ", groupid, webpartid);
            return db.ExecuteObject<Button_List>(sql);
        }

        public int ButtonAuth(int groupid, int webpartid, IList<int> btns)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_groupid",OracleDbType.Int32),
                new OracleParameter("p_webpartid",OracleDbType.Int32),
                new OracleParameter("p_btns",OracleDbType.Varchar2,4000),
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input;
                parameters[2].Direction = ParameterDirection.Input;
                parameters[3].Direction = ParameterDirection.Output;

                parameters[0].Value = groupid;
                parameters[1].Value = webpartid;
                parameters[2].Value = string.Join(",", btns.Select(i => i.ToString()).ToArray());

                db.ExecuteNonQuery("gpm.proc_button_auth", parameters, CommandType.StoredProcedure);
                errmsg = parameters[3].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int ButtonUnAuth(int groupid, int webpartid, IList<int> btns)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_groupid",OracleDbType.Int32),
                new OracleParameter("p_webpartid",OracleDbType.Int32),
                new OracleParameter("p_btns",OracleDbType.Varchar2,4000),
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input;
                parameters[2].Direction = ParameterDirection.Input;
                parameters[3].Direction = ParameterDirection.Output;
                parameters[0].Value = groupid;
                parameters[1].Value = webpartid;
                parameters[2].Value = string.Join(",", btns.Select(i => i.ToString()).ToArray());

                db.ExecuteNonQuery("gpm.proc_button_unauth", parameters, CommandType.StoredProcedure);
                errmsg = parameters[3].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
